package client;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Label;

import javax.swing.*;

import java.awt.Font;

import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.border.EmptyBorder;

import com.google.gson.Gson;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class clientTwo extends JFrame {

	private JPanel contentPane;
	private static int serverPort = 9999;	//port
	private static InetAddress serverIp ;	//IP
	static Socket clientSocket;				// 客戶端socket
	static String tmp;						// 暫存文字訊息
	private static JTextField nameField;	//使用者
	private static JTextField messageField;//訊息輸入入
	private static JTextArea txtMessage;
	
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					clientTwo frame = new clientTwo();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		//執行執行緒與讀取訊息
		Thread t = new Thread(readData);
		t.start();
	}

	public static void onWaitUserType() {
		try {
			serverIp = InetAddress.getByName("192.168.132.1");
			clientSocket = new Socket(serverIp, serverPort);
			if(clientSocket.isConnected()){				
				BufferedWriter bw;
				// 取得網路輸出串流
				bw = new BufferedWriter( new OutputStreamWriter
						(clientSocket.getOutputStream(),"UTF-8"));	
				//取得目前輸入資訊
				bw.write(getGsonString()+"\n");
				// 立即發送
				bw.flush();
				//清空輸入資料
				messageField.setText("");
				
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	
	public static String getGsonString() {
		//時間
		SimpleDateFormat sdFormat = new SimpleDateFormat("a hh:mm:ss");
		Date today = new Date();
		//使用者
		String name = nameField.getText();
		//訊息		
		String message = messageField.getText();		
		Message msg = new Message(name, message ,sdFormat.format(today));
		Gson gson = new Gson();	
		// 將Message物件轉成JSON
		String json = gson.toJson(msg);
		return json;
	}
	
	// 取得網路訊息，這邊不需要改
	private static Runnable readData = new Runnable() {
		public void run() {			
			try {
				// 以內定(本機電腦端)IP為Server端
				serverIp = InetAddress.getByName("192.168.132.1");
				clientSocket = new Socket(serverIp, serverPort);			
				// 取得網路輸入串流
				BufferedReader br = new BufferedReader(new InputStreamReader(
						clientSocket.getInputStream()));
				
				// 當連線後如果有訊息進來,會更新
				while (clientSocket.isConnected()) {					
					// 取得網路訊息
					System.out.println();
					tmp = br.readLine();					
					// 如果不是空訊息
					if(tmp!=null){
						// 顯示新的訊息
						System.out.println(tmp+"\n");
						txtMessage.append(tmp+"\n");
					}
				}
			} catch (IOException e) {
				
			}
		}
	};

	/**
	 * Create the frame.
	 */
	public clientTwo() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 500);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));
		
		JSplitPane splitPane = new JSplitPane();
		contentPane.add(splitPane, BorderLayout.SOUTH);
		
		JPanel panel = new JPanel();
		splitPane.setLeftComponent(panel);
		panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
		JLabel lblName = new JLabel("使用者");
		lblName.setFont(new Font("新細明體", Font.PLAIN, 18));
		panel.add(lblName);
		nameField = new JTextField("JavaSwing");
		nameField.setFont(new Font("新細明體", Font.PLAIN, 18));
		nameField.setColumns(10);
		panel.add(nameField);
		
		JPanel panel_1 = new JPanel();
		splitPane.setRightComponent(panel_1);
		panel_1.setLayout(new BoxLayout(panel_1, BoxLayout.X_AXIS));
		messageField = new JTextField();
		messageField.setText("my Swing Message");
		messageField.setFont(new Font("新細明體", Font.PLAIN, 18));
		nameField.setColumns(10);
		panel_1.add(messageField);
		
		JButton btnNewButton = new JButton("送出");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				onWaitUserType();
			}
		});
		
		btnNewButton.setFont(new Font("新細明體", Font.PLAIN, 18));
		panel_1.add(btnNewButton);
		
		JScrollPane scrollPane = new JScrollPane();
		contentPane.add(scrollPane, BorderLayout.CENTER);
		
		txtMessage = new JTextArea();
		txtMessage.setFont(new Font("新細明體", Font.PLAIN, 16));
		txtMessage.setRows(15);
		scrollPane.setViewportView(txtMessage);
		
		
	}

	
}
